//
//  MoviePlayView.m
//  Common
//
//  Created by xiong qi on 13-4-18.
//  Copyright (c) 2013年 xiong qi. All rights reserved.
//

#import "MoviePlayView.h"

@implementation MoviePlayView
@synthesize resourcePath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.autoresizesSubviews = YES;
        imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        [self addSubview:imageview];
        [imageview release];
        [imageview setBackgroundColor:[UIColor blackColor]];
        imageview.contentMode = UIViewContentModeScaleAspectFit;
        imageview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        imageview.userInteractionEnabled = YES;
        
        
        startBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 80)];
        [imageview addSubview:startBtn];
       // [startBtn release];
        [startBtn setImage:[UIImage imageNamed:@"viewplayer"] forState:UIControlStateNormal];
        [startBtn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        startBtn.center = imageview.center;
        
        [imageview addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    }
    return self;
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
	//实现相关操作
	UIView * currentview = [change objectForKey:NSKeyValueChangeNewKey];
    if (currentview != nil) {
        startBtn.center = imageview.center;
    }
    
}

-(void)SetPlayPath:(NSString *)path
{
    self.resourcePath = path;
    moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:path]];
    moviePlayer.view.frame = self.bounds;
    moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self insertSubview:moviePlayer.view belowSubview:imageview];
    moviePlayer.shouldAutoplay =NO;
    moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    currentinterval = 0;
    playing = NO;
    [self performSelector:@selector(SetThumbnailImage) withObject:nil afterDelay:0];
}

-(void)SetThumbnailImage
{
    imageview.image = [self GetMovieImage:currentinterval];
    [self bringSubviewToFront:imageview];
}

-(void)onClick:(UIButton *)sender
{
    if (playing) {
        [self Pause];
    }
    else
    {
        
        [self Play];
    }
}

-(UIImage *)GetMovieImage:(NSTimeInterval)interval
{
    UIImage * image = [moviePlayer thumbnailImageAtTime:interval timeOption:MPMovieTimeOptionExact];
    return image;
}

-(UIImage *)GetCurrentMovieImage
{
    currentinterval = moviePlayer.currentPlaybackTime;
    UIImage * image = [moviePlayer thumbnailImageAtTime:currentinterval timeOption:MPMovieTimeOptionExact];
    return image;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}

-(void)Play
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:self.resourcePath]) {
        NSString* fileName = [[self.resourcePath componentsSeparatedByString:@"/"] lastObject];
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"资源未下载"
                                                        message:[NSString stringWithFormat:@"请到设置中下载视频资源:%@", [[fileName componentsSeparatedByString:@"."] objectAtIndex:0]]
                                                       delegate:nil
                                              cancelButtonTitle:@"我知道了"
                                              otherButtonTitles:nil, nil];
        [alert show];
        SafeRelease(alert);
        
        return;
    }
    
    if (moviePlayer) {
        [self bringSubviewToFront:moviePlayer.view];
        [moviePlayer play];
        playing = YES;
        
    }
}

-(void)Pause
{
    if (moviePlayer) {
        [moviePlayer pause];
        currentinterval = moviePlayer.currentPlaybackTime;
        playing = NO;
        [self performSelector:@selector(SetThumbnailImage) withObject:nil afterDelay:0];
        
    }
}

-(void)Stop
{
    if (moviePlayer) {
        [moviePlayer stop];
    }
}

-(void)dealloc
{
    [imageview removeObserver:self forKeyPath:@"frame"];
    if (moviePlayer) {
        [moviePlayer.view removeFromSuperview];
        [moviePlayer release];
    }
    self.resourcePath = nil;
    [super dealloc];
}

@end
